#include <QtSql>
#include <assert.h>
int main(int argc, char *argv[])
{
	QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
	qDebug() << "Available drivers:";
	QStringList drivers = QSqlDatabase::drivers();
	foreach(QString driver, drivers)
		qDebug() << "\t" << driver;
	qDebug() << "ODBC driver valid?" << db.isValid();
	db.setDatabaseName("mssql");//the name of the DSN you have cretated int the odbc.ini
	db.setUserName("sa");
	db.setPassword("mark0931");
	//Host is not relevant to DSNs
	//QT will create an ODBC connection string of
	//DSN=mssql;UID=sa;PWD=mark0931
	qDebug() << "connecting";
	if (!db.open()) {
		qDebug() << db.lastError();
		return 0;
	} 
	qDebug() << "success";
	QSqlQuery query(db);
	QString sql = "use big3;exec ap_test ?";
	bool ok = query.prepare(sql);//must have a ? or return false
	if (ok != true)
		qDebug() << query.lastError();
	else {
		QByteArray var;			
		var.push_back('t');
		var.push_back('e');
		var.push_back('s');
		var.push_back('t');
		var.push_back('1');
		query.addBindValue(var);//only QByteArray works on linux version

		ok = query.exec();
		if (ok != true)
			qDebug() << query.lastError();
	}
	query.setForwardOnly(true);//must set true or query fail on linux
	ok = query.exec("exec ap_test");
	if (!ok)
		qDebug() << query.lastError() << "at " << QString::number(__LINE__);
	assert(ok == true);
	assert(query.isSelect() == true);
	ok = query.first();
	assert(ok == true);
	QSqlRecord r = query.record();
	int count = query.size();
	qDebug() << "record count=" << QString::number(count);
	int index = r.indexOf("id");
	assert(index >= 0);
	QString id = r.value(index).toString();//store procedure must convert varchar(n) to binary(n) on linux version qt
	QByteArray ar = r.value(index).toByteArray();
	qDebug() << "length=" << ar.length();	
	qDebug() << "result date=" << id;
	db.close();
	return 0;
}
